package com.dungeon.game;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.dungeon.game.Party.Character;

public class ActionChooser extends RelativeLayout {

	static int action_number;
	Context c;
	LinearLayout buttons0, buttons1, buttons2;

	public ActionChooser(Context context) {
		super(context);
		c = context;
	}

	public ActionChooser(Context context, AttributeSet attrs) {
		super(context, attrs);
		c = context;
	}

	public ActionChooser(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		c = context;
	}

	void setup(){
		action_number = -1;
		buttons0 = (LinearLayout)findViewById(R.id.buttons0);
		buttons0.removeAllViews();
		buttons1 = (LinearLayout)findViewById(R.id.buttons1);
		buttons1.removeAllViews();
		buttons2 = (LinearLayout)findViewById(R.id.buttons2);
		buttons2.removeAllViews();
		Character c = Gamestate.actor;
		ArrayList<Integer> rows = new ArrayList<Integer>();
		ArrayList<Integer> cols = new ArrayList<Integer>();
		int count=0, row=0, col=0;
		for (Character_action a : c.actions){
			if (a.callable()){
				if (col==2){
					row = row + 1;
					col = 0;
				} else {
					col = col + 1;
				}
				rows.add(row);
				cols.add(col);
			} else {
			    rows.add(-1);
			    cols.add(-1);
			}
			count = count + 1;
		}
		for (int ct=c.actions.size()-1; ct>=0; ct--){
			if (cols.get(ct)>=0){
				Character_action a = c.actions.get(ct);
			    Button b = new ActionButton(c,a,ct,this);
			    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80);
		        b.setLayoutParams(params);
		        b.setGravity(Gravity.CENTER);
			    if (cols.get(ct)==0)
			    	buttons0.addView(b);
			    else if (cols.get(ct)==1)
			    	buttons1.addView(b);
			    else if (cols.get(ct)==2)
			    	buttons2.addView(b);
			}
		}
		setOnClickListener(new ReturnOnClickListener(this));
	}

	void refresh(){
	    for (int i = 0; i < buttons0.getChildCount(); i++) {
	        View v = buttons0.getChildAt(i);
	        if (v.getClass() == ActionButton.class) {
	        	((ActionButton)buttons0.getChildAt(i)).refresh();
	        }
	    }
	    for (int i = 0; i < buttons1.getChildCount(); i++) {
	        View v = buttons1.getChildAt(i);
	        if (v.getClass() == ActionButton.class) {
	        	((ActionButton)buttons1.getChildAt(i)).refresh();
	        }
	    }
	    for (int i = 0; i < buttons2.getChildCount(); i++) {
	        View v = buttons2.getChildAt(i);
	        if (v.getClass() == ActionButton.class) {
	        	((ActionButton)buttons2.getChildAt(i)).refresh();
	        }
	    }
    }

	public class ReturnOnClickListener implements OnClickListener {

		ActionChooser ac;

		public ReturnOnClickListener(ActionChooser _ac){
			ac = _ac;
		}

		public void onClick(View v){
           ac.action_number = -99;
        }
	}

    public class ActionButton extends Button {

    	ActionChooser ac;
    	Character_action a;
    	boolean valid;
    	Double cooldown_prop;
    	Paint p;
    	Rect r;

    	public ActionButton(Character c, Character_action _a, int count, ActionChooser ac){
    		super(ac.c);
    		a = _a;
    		getBackground().setColorFilter(a.color().get_color(),PorterDuff.Mode.MULTIPLY);
    		String text;
    		if (a.limited_uses!=null && a.limited_uses>0){
    			text = a.name() + " (" + Integer.toString(a.limited_uses) + ")";
    		} else {
    			text = a.name();
    		}
		    setText(text);
			setOnClickListener(new MyOnClickListener(this,count));
			p = new Paint();
			p.setColor(Color.rgb(210,50,50));
			valid = false;
			getBackground().setColorFilter(null);
			refresh();
    	}

        void refresh(){
    		if (a.ok()){
				setEnabled(true);
    			if (!valid){
    	    		getBackground().setColorFilter(a.color().get_color(),PorterDuff.Mode.SRC_ATOP);
    	    		valid = true;
    			}
    		} else {
    			setEnabled(false);
    			if (valid){
    	    		getBackground().setColorFilter(null);
    	    		valid = false;
    			}
    		}
    		if (a.get_cooldown()!=null && a.get_cooldown()>0 && a.last_used!=null && a.last_used>0){
    			if (a.last_used+a.get_cooldown()>Gamestate.w.clock){
    				cooldown_prop = 1-(Gamestate.w.clock-a.last_used)/a.get_cooldown();
    			}
    		} else {
    			cooldown_prop = null;
    		}
    		invalidate();
        }

        protected void onDraw(Canvas c){
        	super.onDraw(c);
        	if (cooldown_prop!=null){
        		r = new Rect(10,5,(int)Math.floor(10+cooldown_prop*(getWidth()-20)),10);
        		c.drawRect(r, p);
        	}
        }

    }

    public class MyOnClickListener implements OnClickListener
    {

      int number;
      ActionButton ac;

      public MyOnClickListener(ActionButton _ac, int _number) {
    	  ac = _ac;
          number = _number;
      }

      public void onClick(View v)
      {
    	  if (ac.valid)
    		  ActionChooser.action_number = number;
	  }

   };
}


